package com.guinea.core.acegi;

import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.userdetails.UserDetails;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

public class UserDetailsImpl implements UserDetails {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5928951901072395260L;

	private Long id;
	private String username;
	private String otherName;
	private String password;
	private GrantedAuthority[] authorities;
	
	public UserDetailsImpl(Long id, String username, String otherName, String password, GrantedAuthority[] authorities) {
		super();
		this.id = id;
		this.username = username;
		this.otherName = otherName;
		this.password = password;
		this.authorities = authorities;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public GrantedAuthority[] getAuthorities() {
		return authorities;
	}


	public void setAuthorities(GrantedAuthority[] authorities) {
		this.authorities = authorities;
	}


	public String getPassword() {
		return password;
	}


	public void setPassword(String password) {
		this.password = password;
	}


	public String getUsername() {
		return username;
	}


	public void setUsername(String username) {
		this.username = username;
	}


	public boolean isAccountNonExpired() {
		return true;
	}


	public boolean isAccountNonLocked() {
		return true;
	}


	public boolean isCredentialsNonExpired() {
		return true;
	}


	public boolean isEnabled() {
		return true;
	}
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof UserDetailsImpl)) return false;

        final UserDetailsImpl userDetailsImpl = (UserDetailsImpl) o;

        if (username != null ? !username.equals(userDetailsImpl.getUsername()) : userDetailsImpl.getUsername() != null) return false;

        return true;
    }

    public int hashCode() {
        return (username != null ? username.hashCode() : 0);
    }

    public String toString() {
        ToStringBuilder sb = new ToStringBuilder(this,
                ToStringStyle.DEFAULT_STYLE).append("username", this.getUsername())
                .append("enabled", this.isEnabled())
                .append("accountExpired",!this.isAccountNonExpired())
                .append("credentialsExpired",!this.isCredentialsNonExpired())
                .append("accountLocked",!this.isAccountNonLocked());

        GrantedAuthority[] auths = this.getAuthorities();
        if (auths != null) {
            sb.append("Granted Authorities: ");

            for (int i = 0; i < auths.length; i++) {
                if (i > 0) {
                    sb.append(", ");
                }
                sb.append(auths[i].toString());
            }
        } else {
            sb.append("No Granted Authorities");
        }
        return sb.toString();
    }

	public String getOtherName() {
		return otherName;
	}

	public void setOtherName(String otherName) {
		this.otherName = otherName;
	}

}
